//
//  SYJiaoCaiView.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2020/2/25.
//  Copyright © 2020年 SanYi. All rights reserved.
//

#import "SYJiaoCaiView.h"
#import "SYTopLabView.h"
#import "SYJiaoCaiCell.h"
#import "SYJiaoCaiDetailView.h"
#import "SYShareBanJiListView.h"
@interface SYJiaoCaiView ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) SYBackTopView *backView;//顶部viwe
@property (strong, nonatomic) SYTopLabView *topLabView;
@property (strong, nonatomic) UITableView *curTableView;//
@property (strong, nonatomic) NSMutableArray *datalistArr;
@property (assign, nonatomic) NSInteger index;;
@property (copy, nonatomic) NSString *packageId;//选择资源包id
@property (strong, nonatomic) SYJiaoCaiDetailView *caiDetailView;
//@property (strong, nonatomic) SYJiaoCaiCell ;
@property (strong, nonatomic) UIView *contView;//功能view
@property (strong, nonatomic) UIButton *nextBtn;//下一步btn
@property (strong, nonatomic) SYShareBanJiListView *shareView;
@property (strong, nonatomic) NSString *groupId;
@end


@implementation SYJiaoCaiView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithHexString:@"#EEEEEE"];
        [self initSubView];
    }
    return self;
}
- (void)initSubView {
    self.index = -1;
    [self addSubview:self.backView];
    [self.backView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.mas_equalTo(50);
    }];
    [self addSubview:self.topLabView];
    [self.topLabView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self.backView.mas_bottom);
        make.height.mas_equalTo(45);
    }];
    
    [self addSubview:self.curTableView];
    [self.curTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self.topLabView.mas_bottom);
        make.bottom.equalTo(self.mas_bottom).offset(-150);
    }];
    
    _datalistArr = [NSMutableArray array];
    
    [_curTableView registerCellWithNIbNames:@[@"SYJiaoCaiCell"]];
   
    [self addSubview:self.contView];
    [self.contView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self.curTableView.mas_bottom);
        make.bottom.equalTo(self.mas_bottom);
    }];
    //下发资源包
    [self.contView addSubview:self.nextBtn];
    [self.nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.equalTo(self.contView);
        make.width.mas_equalTo(160);
        make.height.mas_equalTo(60);
    }];
    
}

- (void)requestDataGroupId:(NSString *)groupId {
    self.groupId = groupId;
    [MBProgressHUD mj_showLoading];
    [SYRequestAPI zy_requestGetGroupPckgeListGroupid:groupId packageName:@"" success:^(NSArray *arr) {
        [MBProgressHUD mj_hideHUD];
        [self.datalistArr addObjectsFromArray:arr];
        [self.curTableView reloadData];
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUD];
        [MBProgressHUD zr_showError:error.domain];
    }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _datalistArr.count;
//    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SYJiaoCaiCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SYJiaoCaiCell" forIndexPath:indexPath];
    NSDictionary *dict = self.datalistArr[indexPath.row];
    cell.sucaibaoLab.text = [dict nilObjectForKey:@"packageName"];
    cell.fenleiLab.text = [dict nilObjectForKey:@"chapterName"];
    cell.ziyuanshuLab.text = [dict nilObjectForKey:@"resourceCount"];//资源数量
    cell.chuangjianrenLab.text = [dict nilObjectForKey:@"createUser"];
    NSString *timeStr = [dict nilObjectForKey:@"createDate"];
    timeStr = [timeStr substringFromIndex:timeStr.length - 3];//先这样写。后期有时间优化。标记 TOD
    cell.chuangjiantimeLab.text = [[dict nilObjectForKey:@"createDate"] stringByReplacingOccurrencesOfString:timeStr withString:@""];
    __weak typeof(self) weakSelf = self;
    cell.block = ^(NSInteger index) {
        //1 资源数量
        NSString *packgeid = nil;
//        packgeid = @"1";
        packgeid = [dict nilObjectForKey:@"resourcePackageId"];
        
        //2 是否选中
        [weakSelf selectIndexPathPackageid:packgeid indexPathRow:indexPath.row];
    };
    if (self.index == indexPath.row) {
        [cell.selectBtn setImage:[UIImage imageNamed:@"banji_select"] forState:UIControlStateNormal];
    } else {
        [cell.selectBtn setImage:[UIImage imageNamed:@"banji_no_select"] forState:UIControlStateNormal];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    _caiDetailView = [[SYJiaoCaiDetailView alloc] initWithFrame:CGRectMake(kScreen_WIDTH, 0, CGRectGetWidth(self.frame), kScreen_HEIGHT-20)];
    [self addSubview:_caiDetailView];
    __weak typeof(self) weakSelf = self;
     NSDictionary *dict = self.datalistArr[indexPath.row];
    NSString *groupid = [dict nilObjectForKey:@"resourcePackageId"];
    [NSObject executeRunloop:^{
        //        先注销了，等有数据了几开注释
            [self.caiDetailView requestDataGroupId:groupid];
    } afterDelay:kAnimate];
    [UIView animateWithDuration:kAnimate animations:^{
        weakSelf.caiDetailView.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), kScreen_HEIGHT-20);
    }];
   
}
//选中了那个cell  资源包id
- (void)selectIndexPathPackageid:(NSString *)packageId indexPathRow:(NSInteger)index {
    self.index = index;
    //记录选择的资源包id选择发送
    self.packageId = packageId;
    [self.curTableView reloadData];
}
//下一步
- (void)nextBtnClick {
    
    if (self.packageId.length == 0) {
        [MBProgressHUD zr_showError:@"请选择要分享的资源包"];
        return;
    }
    
    _shareView = [[SYShareBanJiListView alloc] initWithFrame:CGRectMake(kScreen_WIDTH, 0, CGRectGetWidth(self.frame), kScreen_HEIGHT-20)];
    [self addSubview:_shareView];
    __weak typeof(self) weakSelf = self;
    [NSObject executeRunloop:^{
        //        先注销了，等有数据了几开注释
                [self.shareView requestDataGroupId:weakSelf.groupId packageId:weakSelf.packageId];
    } afterDelay:kAnimate];
    [UIView animateWithDuration:kAnimate animations:^{
        weakSelf.shareView.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), kScreen_HEIGHT-20);
    }];
}


//头部
- (SYBackTopView *)backView {
    if (!_backView) {
        _backView = [[SYBackTopView alloc] initWithTitleName:@"教材包" baseView:self];
    }
    return _backView;
}
#pragma mark -
- (SYTopLabView *)topLabView {
    if (!_topLabView) {
        NSArray *xiaoArr = nil;
        CGFloat bili;
        
        xiaoArr = @[@"素材包名称",@"分类名称",@"资源数量",@"创建人",@"创建时间",@"是否发送"];
        bili = 0.1666666666;
        
        
        _topLabView = [[SYTopLabView alloc] initWithDataNameArr:xiaoArr width:bili];
    }
    return _topLabView;
}

- (UITableView *)curTableView {
    if (!_curTableView) {
        _curTableView = [[UITableView alloc] init];
        _curTableView.delegate = self;
        _curTableView.dataSource = self;
        _curTableView.tableFooterView = [UIView new];
        
    }
    return _curTableView;
}

- (UIView *)contView {
    if (!_contView) {
        _contView = [[UIView alloc] init];
        _contView.backgroundColor = [UIColor whiteColor];
    }
    return _contView;
}
//下一步but
- (UIButton *)nextBtn {
    if (!_nextBtn) {
        _nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _nextBtn.layer.borderWidth = 1;
        _nextBtn.layer.borderColor = [UIColor colorWithHexString:@"#20B097"].CGColor;
        _nextBtn.layer.cornerRadius = 5;
        _nextBtn.layer.masksToBounds = YES;
        [_nextBtn addTarget:self action:@selector(nextBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [_nextBtn setTitle:@"选择分享班级" forState:UIControlStateNormal];
//        [_nextBtn setTintColor:[UIColor colorWithHexString:@"#20B097"]];
        [_nextBtn setTitleColor:[UIColor colorWithHexString:@"#20B097"] forState:UIControlStateNormal];
        _nextBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    }
    return _nextBtn;
}


@end
